package com.lance.answer.question_answer.xj_new;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lance.answer.dto.XjStudentCourseInfo;
import com.lance.answer.dto.XjStudentCourseInfo.ContentDTO.ItemsDTO;
import com.lance.answer.gui.answer.AnswerObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * 处理获取的新疆一体化的学生课程信息
 */
public class DealXjStudentCourseInfo {

    public static void main(String[] args) throws Exception {


        File directory = new File("E:\\XjStudentCourseInfoTmp\\deal");

        // key = openCourseId  value = 课程中文名
        Map<Integer,String> courseInfo =Maps.newHashMap();


        // key = 用户名
        Map<String, List<ItemsDTO>> mapByUsername = Maps.newHashMap();

        // key = 课程ID  value = 用户集合
        Map<Integer, List<String>> mapByCourseId = Maps.newHashMap();

        for (File file : directory.listFiles()) {
            InputStream inputStream = new FileInputStream(file);
            String answerStr = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

            List<ItemsDTO> list = JSONObject.parseArray(answerStr, ItemsDTO.class);
            List<ItemsDTO> dealList = Lists.newLinkedList();
            for (ItemsDTO itemsDTO : list) {


//                if ("省".equals(itemsDTO.getTestunit())) {
//                    continue;
//                }
             //   if (!Strings.isNullOrEmpty(itemsDTO.getGkLink()) && itemsDTO.getGkLink().contains("student.ouchn")) {


                if (itemsDTO.getPublished()==null||itemsDTO.getPublished()==0) {
                    continue;
                }

                // "toGk":0 为0好像也是没有！！！！！！！！！12-26 FIXME
                if(itemsDTO.getToGk()==1){
                  continue;
                }
//                if(!"心理健康".equals(itemsDTO.getCourseName())){
//                    continue;
//                }
                //FIXME
                dealList.add(itemsDTO);
                courseInfo.put(itemsDTO.getOpenCourseId(),itemsDTO.getCourseName());
            }
            String fileName = file.getName();
            String username = fileName.substring(0, fileName.length() - 5);
            mapByUsername.put(username, dealList);
        }

        for (Map.Entry<String, List<ItemsDTO>> entry : mapByUsername.entrySet()) {

            String username = entry.getKey();

            List<ItemsDTO> itemsDTOList = entry.getValue();

            for (ItemsDTO itemsDTO : itemsDTOList) {
                Integer openCourseId = itemsDTO.getOpenCourseId();
                List<String> usernameLists = mapByCourseId.get(openCourseId);
                if (usernameLists == null) {
                    usernameLists = Lists.newArrayList();
                    mapByCourseId.put(openCourseId, usernameLists);
                }
                usernameLists.add(username);
            }
        }

        List<Map.Entry<Integer, List<String>>> sortList = new ArrayList<>(mapByCourseId.entrySet());

        Collections.sort(sortList, Comparator.comparingInt(o -> o.getValue().size()));

//        for (Map.Entry<Integer, List<String>> entry : mapByCourseId.entrySet()) {
//            System.err.println("key=" + entry.getKey() + "   value:" + entry.getValue().size());
//        }

                for (Map.Entry<Integer, String> entry : courseInfo.entrySet()) {
                    //
        //    System.err.println("key=" + entry.getKey() + "   value:" + entry.getValue());
            System.err.println(entry.getKey() + "&" + entry.getValue());
        }

        for (int i = 0; i < sortList.size(); i++) {
            Map.Entry<Integer, List<String>> entry = sortList.get(i);
            int openCourseId = entry.getKey();
            List<String> usernames = entry.getValue();
            // E:\XjStudentCourseInfoTmp\names
            FileUtils.write(new File("E:\\XjStudentCourseInfoTmp\\names\\"  + openCourseId+".json"), JSONObject.toJSONString(usernames, SerializerFeature.PrettyFormat), "utf-8");

        }


    }

}
